package com.hung.pro.iFlashCards.component;

import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

/**
 * Created by IntelliJ IDEA.
 * User: hung
 * Date: 2008/05/08
 * Time: 10:48:01
 * To change this template use File | Settings | File Templates.
 */
public class CustomSizeLabel extends JLabel {
    String text;
    Font font;
    final int PAD = 20;
    final float SIZE = 6.0f;

    public CustomSizeLabel(String text) {
        this.text = text;
        //use default font name
        font = new Font("", Font.PLAIN, 24);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (text == null || "".equals(text)) {
            return;
        }
        findFontSize();
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setFont(font);
        FontRenderContext frc = g2.getFontRenderContext();
        AttributedString as = new AttributedString(text);
        as.addAttribute(TextAttribute.FONT, font);
        AttributedCharacterIterator iterator = as.getIterator();
        LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc);
        float wrappingWidth = getWidth() - 2 * PAD;
        float x = 0, y = 0;
        while (measurer.getPosition() < text.length()) {
            TextLayout layout = measurer.nextLayout(wrappingWidth);
            y += layout.getAscent();
            float dx = layout.isLeftToRight() ?
                    0 : wrappingWidth - layout.getAdvance();
            layout.draw(g2, x + dx, y);
            y += layout.getDescent() + layout.getLeading();
        }
    }

    private void findFontSize() {
        int h = getHeight();
        float y = 0;
        Font lastFont = font;
        float size = SIZE;
        while (y + PAD <= h) {
            y = PAD;
            Font testFont = font.deriveFont(++size);
            Graphics2D g2 = (Graphics2D) getGraphics();
            g2.setFont(testFont);
            FontRenderContext frc = g2.getFontRenderContext();
            AttributedString as = new AttributedString(text);
            as.addAttribute(TextAttribute.FONT, testFont);
            AttributedCharacterIterator iterator = as.getIterator();
            LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc);
            float wrappingWidth = getWidth() - 2 * PAD;
            float x = PAD;
            while (measurer.getPosition() < text.length()) {
                TextLayout layout = measurer.nextLayout(wrappingWidth);
                y += layout.getAscent();
                float dx = layout.isLeftToRight() ?
                        0 : wrappingWidth - layout.getAdvance();
                y += layout.getDescent() + layout.getLeading();
            }
            if (y + PAD <= h)
                lastFont = testFont;
        }
        font = lastFont;
        //repaint();
    }

    public void updateText(String text) {
        this.text = text;
        revalidate();
        repaint();
    }
}
